/******************************************************************
*
* Copyright 2010 Darshan M Sonde
*
* This file is part of ddots.
*
* ddots is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ddots is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ddots.  If not, see <http://www.gnu.org/licenses/>.
*
*********************************************************************/

#include <QtGui>
#include <QtDebug>

#include "gamescene.h"
#include "dialog.h"

Rohmbus::Rohmbus(QGraphicsItem *parent):
        QGraphicsPolygonItem(parent),
        mOn(true),
        mRowType(true),
        mDash(0)
{
   setAcceptHoverEvents(true);
   createLine();
}

Rohmbus::Rohmbus(const QPolygonF &p,QGraphicsItem *parent):
        QGraphicsPolygonItem(p,parent),
        mOn(true),
        mRowType(true),
        mDash(0)
{
    setAcceptHoverEvents(true);
    createLine();
}
void Rohmbus::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {
        //qDebug() << "got " <<mStartPoint<<mEndPoint;
        if(mOn) {
            emit clicked(mStartPoint,mEndPoint);
            setOn(false);
            //setBrush(QBrush());
        }
    }
}

void Rohmbus::setOn(bool on)
{
    if(mOn!=on) {
        mOn = on;
        //state change
        if(on) {
            //off -> on
            QPen pen;
            pen.setBrush(HIGHLIGHT_LINE_COLOR);
            mDash->setPen(pen);//default width
        } else {
            //on -> off

            //increase width.
            QPen pen;
            int lineWidth = qMin(sceneBoundingRect().width(),sceneBoundingRect().height())/LINEWIDTH;
            pen.setWidth(lineWidth);
            pen.setBrush(QColor(LINECOLOR));
            mDash->setPen(pen);           
        }
    }
}
void Rohmbus::setRowType(bool isRowType)
{

    mRowType = isRowType;

    QRectF rect=boundingRect();
    QPointF start,end;
    if(mRowType) {
        //horizontal
        start=rect.topLeft()+QPointF(0,rect.height()/2);
        end=rect.bottomRight()-QPointF(0,rect.height()/2);
    } else {
        start=rect.topLeft()+QPointF(rect.width()/2,0);
        end=rect.bottomRight()-QPointF(rect.width()/2,0);
    }

    mDash->setLine(QLineF(start,end));

}

void Rohmbus::createLine()
{
    //on -> off
    //no more mouse events. now have to draw a line
    QRectF rect=boundingRect();
    QPointF start,end;
    if(mRowType) {
        //horizontal
        start=rect.topLeft()+QPointF(0,rect.height()/2);
        end=rect.bottomRight()-QPointF(0,rect.height()/2);
    } else {
        start=rect.topLeft()+QPointF(rect.width()/2,0);
        end=rect.bottomRight()-QPointF(rect.width()/2,0);
    }
    if(!mDash)
        mDash=new QGraphicsLineItem(QLineF(start,end),this);
    mDash->setPen(Qt::NoPen);
}

void Rohmbus::hoverEnterEvent( QGraphicsSceneHoverEvent *event )
{
    Q_UNUSED(event);
    if(mOn) {
        QPen pen;
        pen.setBrush(Qt::green);
        mDash->setPen(pen);//default width
    }
}

void Rohmbus::hoverLeaveEvent( QGraphicsSceneHoverEvent *event )
{
    Q_UNUSED(event);
    if(mOn) {
        mDash->setPen(Qt::NoPen);//default width
    }
}

